<!-- subpkg_archive/list/index.vue -->
<script setup>
	import { ref } from 'vue'
	import { deletePatientAPI, patientListAPI } from '@/apis/user'
	import { onShow } from '@dcloudio/uni-app'
	const swipeOptions = ref([
		{
			text: '删除',
			style: {
				backgroundColor: '#dd524d'
			}
		}
	])
	// 保存患者列表
	const patientList = ref([])
	// 是否显示患者
	const patientShow = ref(false)
	const getPatientList = async () => {
		const res = await patientListAPI()
		patientShow.value = true
		patientList.value = res.data
	}

	onShow(() => {
		getPatientList()
	})
	// 删除
	const onDel = (id) => {
		// 提示
		uni.showModal({
			content: '您确定要删除吗?',
			success: async (res) => {
				if (res.confirm){
					// 调用删除的接口函数
					await deletePatientAPI(id)
					// 重新获取数据
					getPatientList()
					// 提示用户
					uni.showToast({
						icon:'success',
						title: '删除成功'
					})
				}
			}
		})
	}
</script>

<template>
	<scroll-page>
		<view class="archive-page" v-if="patientShow">
			<view class="archive-tips">最多可添加6人</view>

			<uni-swipe-action>
				<uni-swipe-action-item
					:right-options="swipeOptions"
					v-for="item in patientList"
					:key="item.id"
					@click="onDel(item.id)"
				>
					<view
						class="archive-card"
						:class="{ active: item.defaultFlag === 1 }"
					>
						<view class="archive-info">
							<text class="name">{{ item.name }}</text>
							<text class="id-card">
								{{
									item.idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '$1********$2')
								}}
							</text>
							<text class="default" v-if="item.defaultFlag === 1">默认</text>
						</view>
						<view class="archive-info">
							<text class="gender">{{ item.genderValue }}</text>
							<text class="age">{{ item.age }}岁</text>
						</view>
						<navigator
							hover-class="none"
							class="edit-link"
							:url="`/subpkg_archive/form/index?id=${item.id}`"
						>
							<uni-icons
								type="icon-edit"
								size="20"
								color="#16C2A3"
								custom-prefix="iconfont"
							/>
						</navigator>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<!-- 添加按钮 -->
			<view v-if="true" class="archive-card">
				<navigator
					class="add-link"
					hover-class="none"
					:url="`/subpkg_archive/form/index?id=10`"
				>
					<uni-icons color="#16C2A3" size="24" type="plusempty" />
					<text class="label">添加患者</text>
				</navigator>
			</view>
		</view>
	</scroll-page>
</template>

<style lang="scss">
	@import './index.scss';
</style>
